"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : WEAK_PASSWORD_DICTIONARY
Case Name   : 系统管理员向系统表插入弱口令
Create At   : 2022/08/12
Owner       : opentestcase004
Description :
    1.创建系统管理员
    2.系统管理员向系统表gs_global_config添加弱口令
    3.查看系统表
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.返回新插入弱口令
    4.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Confirguration(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user = 'u_alter_global_configuration_0015'

    def test_alter_global_configuration(self):
        step = 'step1:创建系统管理员 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user} cascade;
            create user {self.user} with sysadmin password 
            '{macro.PASSWD_REPLACE}';''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:系统管理员向系统表gs_global_config插入弱口令 ' \
               'expect:成功'
        self.logger.info(step)
        alter_configuration = self.primary_sh.execut_db_sql(
            f'''create weak password dictionary ('test_123A'),
            ('test1_234W');''',
            sql_type=f" -U {self.user} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(alter_configuration)
        self.assertIn('CREATE WEAK PASSWORD DICTIONARY', alter_configuration,
                      '执行失败' + step)

        step = 'step3:查看系统表 expect:返回新插入弱口令'
        self.logger.info(step)
        select_configuration = self.primary_sh.execut_db_sql(
            f'''select * from gs_global_config;''')
        self.logger.info(select_configuration)
        self.assertIn('test_123A', select_configuration, '执行失败' + step)
        self.assertIn('test1_234W', select_configuration, '执行失败' + step)

    def tearDown(self):
        step = 'step4:清理环境 expect:成功'
        self.logger.info(step)
        drop_weak = self.primary_sh.execut_db_sql(
            f'''drop weak password dictionary;''',
            sql_type=f" -U {self.user} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(drop_weak)
        drop_user = self.primary_sh.execut_db_sql(
            f'''drop user {self.user};''')
        self.logger.info(drop_user)
        self.assertIn('DROP WEAK PASSWORD DICTIONARY', drop_weak, '执行失败' +
                      step)
        self.assertIn(self.Con.DROP_ROLE_SUCCESS_MSG, drop_user, '执行失败' +
                      step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
